﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GGJ.Sprites
{
    public enum InsecticideState
    {
        NotUsed = 0,
        InUse,
        Used
    }

    class Insecticide : Ennemy
    {
        private int tileWidth = 52;
        private int tileHeight = 190;

        private int fogTileWidth = 78;
        private int fogTileHeight = 71;

        private int fogDetectionSpace = 20;

        Rectangle fogDestination, fogDetectionDestination;

        private double fogTime = 0.0f;

        private int fallSpeed = 5;

        private InsecticideState state = InsecticideState.NotUsed;

        public Insecticide(Texture2D texture, int x, int y)
            : base(texture)
        {
            destination = new Rectangle(
                x,
                y,
                tileWidth,
                tileHeight);

            fogDestination = new Rectangle(
                x - fogTileWidth,
                y,
                fogTileWidth,
                fogTileHeight);

            fogDetectionDestination = new Rectangle(
                x - fogTileWidth - fogDetectionSpace,
                y,
                fogTileWidth,
                fogTileHeight);
        }

        public override void Update(GameTime gameTime, Flies flies,KilledFlies killedFlies,float speed, bool isSlowMotion)
        {
            if (state == InsecticideState.Used)
            {
              
                destination.Y += (int)speed;
            }
            else
            {
                if (isSlowMotion)
                {
                    destination.X -= (int)(speed);
                    fogDestination.X -= (int)speed;
                    fogDetectionDestination.X -= (int)(speed);
                }
                   
                else
                {
                    fogDetectionDestination.X -= (int)speed;
                    fogDestination.X -= (int)speed;
                    destination.X -= (int)speed;
                }
                   
 
                if (state == InsecticideState.InUse)
                {
                    fogTime += gameTime.ElapsedGameTime.TotalSeconds;
                    if (fogTime > 0.5f)
                    {
                        fogTime = 0.0f;
                        state = InsecticideState.Used;
                    }
                }

                Fly fly;

                for (int i = 0; i < flies.Count(); i++)
                {
                    fly = flies.GetFly(i);

                    if (fly != null)
                    {

                        if (state == InsecticideState.InUse)
                        {
                            if (fogDestination.Intersects(fly.GetDestination()))
                            {
                                flies.removeFly(i);
                                fly.isDead = true;
                                fly.causeOfDeath = FliesMovement.Intoxicated;
                                killedFlies.AddFly(fly);
                            }
                        }
                        else
                        {
                            if (fogDetectionDestination.Intersects(fly.GetDestination()))
                            {
                                Program.spray.Play();
                                state = InsecticideState.InUse;
                            }
                        }
                    }
                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Rectangle source, fogSource;

            source = new Rectangle(137,
                8,
                tileWidth,
                tileHeight);

            spriteBatch.Draw(texture, destination, source, Color.White);

            if (state == InsecticideState.InUse)
            {
                fogSource = new Rectangle(192,
                    3,
                    fogTileWidth,
                    fogTileHeight);

                spriteBatch.Draw(texture, fogDestination, fogSource, Color.White);
            }
        }
    }
}
